set linesize 140
set rmsg on
set scheme s1mono
/***************************************************************************
	Project		:	Army Service in the All-Volunteer Era	
	Author(s)	:	Kyle Greenberg	(kyle.greenberg@westpoint.edu)
					Matthew Gudgeon (matthew.gudgeon@westpoint.edu)
					Adam Isen 		(Adam.Isen@treasury.gov)
					Corbin Miller 	(Corbin.Miller@treasury.gov)
					Rich Patterson 	(rich_patterson@byu.edu)
	File Name	:	table_a_21_a_22.do
	Description	:	This file runs the analysis for table a.21 and table a.22
****************************************************************************/

*---- if not running master, set up file structure ----*
*change directory to where programs and subfolders are stored
*cd ""

if "${raw}"==""		global raw		"raw/"
if "${data}"==""	global data		"data/"
if "${output}"==""	global output	"output/"

cap mkdir	"${output}"

use "${data}army-treasury-analysis", clear

keep if white==1 | black==1
keep if inrange(firstafqt,12,49) |  inrange(firstafqt,31,68)

keep pid  white black army_19 army_reserve_19 nonarmy_active_19 nonarmy_reserve_19 anymilitary_19 anymilitary_19  access firstyear wages_19 firstafqt  k31  instk31 k31_2 instk31_2 inst31  k50  instk50 k50_2 instk50_2 inst50 quarterFE

drop if wages_19==.

merge 1:1 tin using "${raw}army_sector", nogen keep(3)

gen any=0 if wage!=.
replace any=1 if average_wage19 !=. & any==0

drop nonprofit0 - average_wage5_18 

replace average_wage19=1.44*average_wage19 if military19==1

set more off

*at21
*type of employment 19 years later

log using "${output}table_a_21", text replace

*sector
gen not_working=0 if  wage!=.
replace not_working=1 if forprofit19==. & wage==0
replace not_working=1 if  wage==0

replace military19=0 if military19==.
replace forprofit19=0 if forprofit19==.
replace gov19=0 if gov19==.
replace nonprofit19=0 if nonprofit19==.

foreach outcome in not_working military19 forprofit19 nonprofit19 gov19 {
	ivregress 2sls `outcome' k31  instk31 k31_2 instk31_2  i.quarter  (access=inst31) if inrange(firstafqt,12,49) & white==1  , r
	ivregress 2sls `outcome' k31  instk31 k31_2 instk31_2  i.quarter  (access=inst31) if inrange(firstafqt,12,49) & white==0 , r

	ivregress 2sls `outcome' k50  instk50 k50_2 instk50_2 i.quarter  (access=inst50) if inrange(firstafqt,31,68)   & white==0 , r 
	ivregress 2sls `outcome' k50  instk50 k50_2 instk50_2 i.quarter  (access=inst50) if inrange(firstafqt,31,68)   & white==1 , r 

}
log close

*at22
*average industry pay

log using "${output}table_a_22", text replace

*average earnings in 6 digit industry using full pop random sample around age 40
ivregress 2sls average_wage19 k31  instk31 k31_2 instk31_2  i.quarter  (access=inst31) if inrange(firstafqt,12,49) & white==1  , r
ivregress 2sls average_wage19 k31  instk31 k31_2 instk31_2  i.quarter  (access=inst31) if inrange(firstafqt,12,49) & white==0  , r

ivregress 2sls average_wage19 k50  instk50 k50_2 instk50_2 i.quarter  (access=inst50) if inrange(firstafqt,31,68)   & white==1 , r 
ivregress 2sls average_wage19 k50  instk50 k50_2 instk50_2 i.quarter  (access=inst50) if inrange(firstafqt,31,68)   & white==0 , r 

*average earnings in 6 digit industry using full pop random sample around age 40
ivregress 2sls average_wage19 k31  instk31 k31_2 instk31_2  i.quarter  (access=inst31) if inrange(firstafqt,12,49) & white==1 & wage!=0  , r
ivregress 2sls average_wage19 k31  instk31 k31_2 instk31_2  i.quarter  (access=inst31) if inrange(firstafqt,12,49) & white==0 & wage!=0  , r

ivregress 2sls average_wage19 k50  instk50 k50_2 instk50_2 i.quarter  (access=inst50) if inrange(firstafqt,31,68)   & white==1 & wage!=0 , r 
ivregress 2sls average_wage19 k50  instk50 k50_2 instk50_2 i.quarter  (access=inst50) if inrange(firstafqt,31,68)   & white==0 & wage!=0 , r 

*control for military
ivregress 2sls average_wage19 k31 military19 instk31 k31_2 instk31_2  i.quarter (access=inst31) if inrange(firstafqt,12,49) & white==1 & wage!=0  , r
ivregress 2sls average_wage19 k31 military19 instk31 k31_2 instk31_2  i.quarter (access=inst31) if inrange(firstafqt,12,49) & white==0 & wage!=0  , r

ivregress 2sls average_wage19 k50 military19 instk50 k50_2 instk50_2 i.quarter (access=inst50) if inrange(firstafqt,31,68)   & white==1 & wage!=0 , r 
ivregress 2sls average_wage19 k50 military19 instk50 k50_2 instk50_2 i.quarter (access=inst50) if inrange(firstafqt,31,68)   & white==0 & wage!=0 , r 

log close
